<?php

namespace app\common\model\shopro;
use think\Model;
use traits\model\SoftDelete;
use app\admin\model\shopro\user\User;
use app\admin\model\shopro\Pay as PayModel;

class OrderAudit extends Model
{
    protected $name = 'shopro_trade_order_audit';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    // 追加属性
    protected $append = [
        'status_text',
        'platform_text',
    ];


    // 订单状态
    const STATUS_CLOSED = 1;
    const STATUS_UNPAID = -1;
    const STATUS_PAID = 0;


    /**
     * 订单列表状态搜索
     */
    public function searchStatusList()
    {
        return [
            -1 => '驳回',      // 包括刚支付的，以及已完成的所有付过款的订单
            0 => '待审核',
            1 => '已公共'
        ];
    }


    public function typeList()
    {
        return [
            'recharge' => '充值订单',
        ];
    }

    public function getStatusTextAttr($value,$data)
    {
        if(isset($data['status'])){
            if($data['status'] == 0){
                $value = '待审核';
            }elseif ($data['status'] == 1){
                $value = '已通过';
            }else{
                $value = '已驳回';
            }
        }
        return $value; // TODO: Change the autogenerated stub
    }
    // 定义获取器，自动格式化时间戳字段
    public function getCreateTimeAttr($value,$data)
    {
        if(isset($data['createtime'])){
            $value = date('Y-m-d H:i:s', $data['createtime']);
        }
        return $value;
    }

    public function platformList()
    {
        return [
            'H5' => 'H5',
            'WechatOfficialAccount' => '微信公众号',
            'WechatMiniProgram' => '微信小程序',
            'App' => 'App',
        ];
    }

    public function getMethodsAttr($value,$data)
    {
       $arr =  ['alipay' => __('支付宝'), 'wechat' => __('微信'), 'usdt' => __('USDT'),'bankcard' => __('银行卡')];
        if(isset($data['methods'])){
            $value = $arr[$data['methods']];
        }
        return $value;
    }


    public function getPlatformTextAttr($value, $data)
    {
        $value = $value ?: ($data['platform'] ?? null);

        $list = $this->platformList();
        return isset($list[$value]) ? $list[$value] : '';
    }






    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function pays()
    {
        return $this->hasMany(PayModel::class, 'order_id', 'id')->typeTradeOrder();
    }
}
